package com.salesforce.samples.templateapp;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONObject;

import com.salesforce.androidsdk.app.SalesforceSDKManager;
import com.salesforce.androidsdk.rest.RestClient;
import com.salesforce.androidsdk.rest.RestRequest;
import com.salesforce.androidsdk.rest.RestResponse;
import com.salesforce.androidsdk.rest.RestClient.AsyncRequestCallback;
import com.salesforce.androidsdk.util.EventsObservable;
import com.salesforce.androidsdk.util.EventsObservable.EventType;
import com.salesforce.samples.templateapp.model.Bean_Class;
import com.salesforce.samples.templateapp.model.Contacts;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class ContactDetailsFragment  extends Fragment implements OnClickListener {
	
	ScrollView svContact,svContactdetails;
	LinearLayout llayout;
	EditText etMaillingCity,etMaillingStreet,etMailingCountry,etMaillingstate,etMaillingZip,etContactAccountName,etOtherCity,etOtherStreet,etOtherState,etOtherCountry,etOtherZip,etFirstName,etLastName,etTitle,etContactphone,etMobile,etEmail,etDescription;
	TextView  tvContactBillingToShipping,tvMaillingCity,tvMaillingStreet,tvMailingCountry,tvMaillingstate,tvMaillingZip,tvContactAccountName,tvOtherCity,tvOtherStreet,tvOtherState,tvOtherCountry,tvOtherZip,
	tvFirstName,tvLastName,tvTitle,tvContactphone,tvMobile,tvEmail,tvDescription;;
	RestClient client;
	Button mEdit,mContactSave;
	Spinner mType,mIndustry,mRating,mOwnerShip,etLeadSource;
	Bean_Class bean;
	ListView lvLookup;
	
	ArrayList<Bean_Class> filterList;
	boolean isFiltered;
	ArrayList<Bean_Class> data;
	Dialog lookupDialog;
	String parantAccountID;
	static ArrayList<Contacts> contactList;
	boolean flag;
	GlobalListener mHomeItemListener;   
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.contact_details_fragment, null);
	}

	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		mHomeItemListener = (GlobalListener) (activity);
	}
	
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		
		svContactdetails = (ScrollView) getView().findViewById(R.id.contactdetails);
		svContact = (ScrollView) getView().findViewById(R.id.contactScrooview);
		llayout = (LinearLayout) getView().findViewById(R.id.llDisplayLayout);
		mEdit = (Button) getView().findViewById(R.id.btEdit);
		mContactSave = (Button) getView().findViewById(R.id.btSave2);
		mType = (Spinner) getView().findViewById(R.id.etType);
		mRating = (Spinner) getView().findViewById(R.id.etRating);
		mIndustry = (Spinner) getView().findViewById(R.id.etIndustry);
		mOwnerShip = (Spinner) getView().findViewById(R.id.etOwnership);
		tvContactBillingToShipping = (TextView)getView().findViewById(R.id.tvCopyContactBillingToShipping);
		
		
		tvContactBillingToShipping.setOnClickListener(this);
		mContactSave.setOnClickListener(this);              
		//mEdit.setOnClickListener(this);
		
		etMailingCountry = (EditText) getView().findViewById(R.id.etMailingcountry);
		etMaillingCity = (EditText) getView().findViewById(R.id.etMailingcity);
		etMaillingstate = (EditText) getView().findViewById(R.id.etMailingState);
		etMaillingStreet = (EditText) getView().findViewById(R.id.etMailingStreet);
		etMaillingZip = (EditText) getView().findViewById(R.id.etMailingcode);
		etContactAccountName = (EditText) getView().findViewById(R.id.etContactAccountName);
		etOtherCity = (EditText) getView().findViewById(R.id.etOtherCity);
		etOtherCountry = (EditText) getView().findViewById(R.id.etOthercountry);
		etOtherState = (EditText) getView().findViewById(R.id.etOtherState);
		etOtherStreet = (EditText) getView().findViewById(R.id.etOtherstreet);
		etOtherZip = (EditText) getView().findViewById(R.id.etOthercode);
		etFirstName = (EditText) getView().findViewById(R.id.etFirstname);
		etLastName = (EditText) getView().findViewById(R.id.etLastname);
		etTitle = (EditText) getView().findViewById(R.id.etTite);
		etMobile = (EditText) getView().findViewById(R.id.etMobile);
		etEmail = (EditText) getView().findViewById(R.id.etEmail);
		etContactphone = (EditText) getView().findViewById(R.id.etContactPhone);
		etLeadSource = (Spinner) getView().findViewById(R.id.etLeadSource);
		etDescription = (EditText) getView().findViewById(R.id.etDescription);
				
		
		tvMailingCountry = (TextView) getView().findViewById(R.id.tvMailingcountry);
		tvMaillingCity = (TextView) getView().findViewById(R.id.tvMailingcity);
		tvMaillingstate = (TextView) getView().findViewById(R.id.tvMailingState);
		tvMaillingStreet = (TextView) getView().findViewById(R.id.tvMailingStreet);
		tvMaillingZip = (TextView) getView().findViewById(R.id.tvMailingcode);
		tvContactAccountName = (TextView) getView().findViewById(R.id.tvContactAccountName);
		tvOtherCity = (TextView) getView().findViewById(R.id.tvOtherCity);
		tvOtherCountry = (TextView) getView().findViewById(R.id.tvOthercountry);
		tvOtherState = (TextView) getView().findViewById(R.id.tvOtherState);
		tvOtherStreet = (TextView) getView().findViewById(R.id.tvOtherstreet);
		tvOtherZip = (TextView) getView().findViewById(R.id.tvOthercode);
		tvFirstName = (TextView) getView().findViewById(R.id.tvFirstname);
		tvLastName = (TextView) getView().findViewById(R.id.tvLastname);
		tvTitle = (TextView) getView().findViewById(R.id.tvTite);
		tvMobile = (TextView) getView().findViewById(R.id.tvMobile);
		tvEmail = (TextView) getView().findViewById(R.id.tvEmail);
		tvContactphone = (TextView) getView().findViewById(R.id.tvContactPhone);
		
		
		
		client = MainActivity.mActivity.client;
		
		
		Bundle args2 = getArguments();
        if (args2 != null) {                 
        	
        	
        	if(args2.getInt("Position")== 1){
        		
        		svContactdetails.setVisibility(View.VISIBLE);
        		svContact.setVisibility(View.GONE);
        		tvLastName.setText(args2.getString("LastName"));           
        		tvFirstName.setText(args2.getString("FirstName"));
        		tvContactphone.setText(args2.getString("Phone"));
        		tvMaillingCity.setText(args2.getString("MailingCity"));
        		tvMailingCountry.setText(args2.getString("MailingCountry"));
        		tvMaillingZip.setText(args2.getString("MailingPostalCode"));
        		tvMaillingstate.setText(args2.getString("MailingState"));
        		tvMaillingStreet.setText(args2.getString("MaillingStreet"));
        		tvOtherCity.setText(args2.getString("OtherCity"));
        		tvOtherCountry.setText(args2.getString("OtherCountry"));
        		tvOtherZip.setText(args2.getString("OtherPostalCode"));      
        		tvOtherState.setText(args2.getString("OtherState"));
        		tvOtherStreet.setText(args2.getString("OtherStreet"));
        		tvEmail.setText(args2.getString("Email"));
        		tvMobile.setText(args2.getString("MobilePhone"));   
        		tvTitle.setText(args2.getString("Title"));
        		
        		
        	}else{
        	svContact.setVisibility(View.VISIBLE);
        	svContactdetails.setVisibility(View.GONE);
        	etMailingCountry.setText(args2.getString("MailingCountry"));
			etMaillingCity.setText(args2.getString("MaillingCity"));
			etMaillingstate.setText(args2.getString("Maillingstate"));
			etMaillingStreet.setText(args2.getString("MaillingStreet"));
			etMaillingZip.setText(args2.getString("MaillingZip"));
			etContactphone.setText(args2.getString("Contactphone"));
			etContactAccountName.setText(args2.getString("ContactAccountName"));
			parantAccountID = args2.getString("AccountId");
        	}
        }
        
	}


	@Override
	public void onClick(View v) {
		
		switch (v.getId()) {
		
			
		case R.id.btSave2:
			createContact();
			break;
			
		case R.id.tvCopyContactBillingToShipping:
			String MaillingCity = etMaillingCity.getText().toString();
			if (MaillingCity != null) {
				etOtherCity.setText(MaillingCity);
			}
			String MailingCountry = etMailingCountry.getText().toString();
			if (MailingCountry != null) {
				etOtherCountry.setText(MailingCountry);
			}

			String MailingZip = etMaillingZip.getText().toString();
			if (MailingZip != null) {
				etOtherZip.setText(MailingZip);
			}
			String MailingState = etMaillingstate.getText().toString();
			if (MailingState != null) {
				etOtherState.setText(MailingState);
			}
			String MailingStreet = etMaillingStreet.getText().toString();
			if (MailingStreet != null) {
				etOtherStreet.setText(MailingStreet);
			}
			
			break;

		default:
			break;
		}
	}
	
	public void createContact(){
		{
			String objectType = "Contact";
			HashMap<String, Object> fields = new HashMap<String, Object>();
			
			fields.put("AccountId",parantAccountID);
			fields.put("FirstName",etFirstName.getText().toString());
			fields.put("LastName",etLastName.getText().toString());
			fields.put("Phone",etContactphone.getText().toString());
			fields.put("MailingCity",etMaillingCity.getText().toString());
			fields.put("MailingState",etMaillingstate.getText().toString());
			fields.put("MailingCountry", etMailingCountry.getText().toString());
			fields.put("MailingPostalCode", etMaillingZip.getText().toString());
			fields.put("MailingStreet", etMaillingStreet.getText().toString());
			fields.put("OtherCity", etOtherCity.getText().toString());
			fields.put("OtherCountry",etOtherCountry.getText().toString());
			fields.put("OtherPostalCode",etOtherZip.getText().toString());
			fields.put("OtherState",etOtherState.getText().toString());
			fields.put("OtherStreet",etOtherStreet.getText().toString());
			fields.put("Email",etEmail.getText().toString());
			fields.put("MobilePhone",etMobile.getText().toString());
			fields.put("Title",etTitle.getText().toString());
			fields.put("LeadSource", etLeadSource.getSelectedItem().toString());
			fields.put("Description", etDescription.getText().toString());
			
			try {
			    RestRequest restRequest = RestRequest.getRequestForCreate(Bean_Class.api_Version,objectType,fields);
			    
			    Log.i("NCS","REquest is  :"+restRequest);
			    
			    client.sendAsync(restRequest, new AsyncRequestCallback() {
					@Override
					public void onSuccess(RestRequest request, RestResponse result) {
						try {     
							Log.i("TPT","Response is   :"+result);
							
							JSONObject json = new JSONObject(result.toString());
							if(json!= null){
								Log.i("TPT","Response is   :"+json.getString("success"));
								if(json.getString("success").equalsIgnoreCase("true")){
									mHomeItemListener.onCategoryClicked(-2);
								}
								
							}
						
							
							}                                
						catch (Exception exception) {
							
						}
					}
					
					//@Override
					public void onError(Exception exception) {
						 Toast.makeText(getActivity(),
			                		ContactDetailsFragment.this.getString(SalesforceSDKManager.getInstance().getSalesforceR().stringGenericError(), exception.toString()),
			                               Toast.LENGTH_LONG).show();
						EventsObservable.get().notifyEvent(EventType.RenditionComplete);
					}
				});
			} 
			catch (Exception e) {
				Log.i("NCS","Exception is   :"+e.getMessage());
			    
			}

		}
		
		
	}
	
}
